// 引入mysql2
const mysql = require('mysql2')
// 创建数据库连接池--配置相关信息
const pool = mysql.createPool({
  host: 'localhost',
  port: 3306,
  user: 'root',
  password: '123456',
  database: 'express-mysql',
  serverTimezone: 'Asia/Shanghai',
})
// sql操作封装
/**
 * 
 * @param {String} sql 
 * @param {Function} callback 
 */
function query (sql, callback) {
  // 创建数据库连接
  pool.getConnection((err, connection) => {
    if (err) {
      console.error("Error connection to MySQL database", err)
      return
    } else {
      console.log("Connectecd to MySQL database Success")
      connection.query(sql, (err, data) => {
        callback(err, data)
        // 释放数据库连接
        connection.release()
      })
    }

  })
}
exports.query = query